package de.szut.sudoku.gui.sudokufield;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JLabel;

import de.szut.sudoku.gui.MainFrame;

/**
 * Listener for clicks on a sudoku button
 * 
 * @author Marc Huisinga
 * 
 */
public class SudokuButtonClickListener extends MouseAdapter {
	private JLabel currentSelectedLabel;
	private Color defaultColor;
	private Color switchColor;

	public SudokuButtonClickListener() {
		defaultColor = SudokuFieldLabel.DEFAULTCOLOR;
		switchColor = SudokuFieldLabel.SWITCHCOLOR;
		currentSelectedLabel = new JLabel();
	}

	public JLabel getCurrentSelectedLabel() {
		return currentSelectedLabel;
	}

	public void setCurrentSelectedLabel(JLabel currentSelectedLabel) {
		this.currentSelectedLabel = currentSelectedLabel;
	}

	/**
	 * Mouse click event
	 */
	@Override
	public void mouseClicked(MouseEvent arg0) {
		JLabel source = (JLabel) arg0.getSource();
		// resets the focus if the source is not the focus owner
		if (!source.isFocusOwner()) {
			source.setBackground(switchColor);
			source.requestFocus();
			if (!source.equals(currentSelectedLabel)) {
				currentSelectedLabel.setBackground(defaultColor);
				currentSelectedLabel = source;
			}
		} else {
			source.setBackground(defaultColor);
			source.transferFocusBackward();
		}
		// refreshes the frame
		MainFrame.getInstance().revalidate();
		MainFrame.getInstance().repaint();
	}

}
